<template>
  <div class="container-box" id="container-box">
    <el-row>
      <el-col :xl="12" :lg="14" :md="16" :sm="16" :xs="18">
        <el-form :model="productForm" status-icon :rules="rules" ref="productForm" label-width="100px">
          <el-form-item label="产品名称：" prop="product_name">
            <el-input v-model="productForm.product_name"></el-input>
          </el-form-item>
          <!-- <el-form-item label="产品价格：" prop="product_price">
            <el-input v-model="productForm.product_price"></el-input>
          </el-form-item>
          <el-form-item label="产品库存：" prop="product_inventory">
            <el-input v-model="productForm.product_inventory"></el-input>
          </el-form-item> -->
          <el-form-item label="状态：" prop="active">
            <el-select v-model="productForm.active" placeholder="请选择产品状态">
              <el-option label="可用" value="1"></el-option>
              <el-option label="禁用" value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('productForm')">立即创建</el-button>
            <el-button @click="resetForm('productForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { timestampToDate } from "../../assets/js/utils";

export default {
  data() {
    return {
      productForm: {
        product_name: "",
        product_price: "",
        product_inventory: "",
        active: ""
      },
      rules: {
        product_name: [{ required: true, message: "请输入账号" }],
        active: [{ required: true, message: "请选择状态", trigger: "change" }]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let token = window.localStorage.getItem("token");
          this.axios
            .post("products?access-token=" + token, this.productForm)
            .then(res => {
              if (res.data.code === "1000") {
                this.$message({
                  type: "success",
                  message: "创建成功!"
                });
                this.resetForm("productForm");
              } else {
                this.$message({
                  type: "error",
                  message: res.data.message
                });
              }
            });
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>

<style lang="scss" scoped>
</style>
